package com.zhuss.opus.processor;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.vo.ListInvitTopicTreeVo;
import com.zhuss.bean.vo.core.append.MessageVo;
import com.zhuss.bean.vo.opus.invit.DetailInvitTopicVo;
import com.zhuss.common.BasePageParam;
import com.zhuss.common.PageVo;
import com.zhuss.enums.Area;
import com.zhuss.enums.IsEnum;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.exception.WpsException;
import com.zhuss.opus.db.dao.ext.InvitTopicDao;
import com.zhuss.util.ListBean;


/**
 * 查询帖子Topic processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("invitTopicRead")
public class InvitTopicRead {
	
	@Autowired
	private InvitTopicDao invitTopicDao;
	
	
	private void updateAlreadyRead(BasePageParam param) {
		Long toUserId = param.getConcurrentUserId();
		invitTopicDao.updateAlreadyRead(toUserId, IsEnum.是.key());
	}
	
	public Integer countNoReadNum(Long toUserId) {
		Integer noReadNum = invitTopicDao.countNoReadNum(toUserId, IsEnum.否.key());
		return noReadNum;
	}
	
	
	
	public MessageVo<DetailInvitTopicVo> listTopicBy(BasePageParam param) {
		MessageVo<DetailInvitTopicVo> result = new MessageVo<>();
		PageVo<DetailInvitTopicVo> pageVo = listBy(param);
		Integer noReadNum = countNoReadNum(param.getConcurrentUserId());
		result.setPageVo(pageVo);
		result.setNoReadNum(noReadNum);
		updateAlreadyRead(param);
		return result;
	}
	
	private PageVo<DetailInvitTopicVo> listBy(BasePageParam param) {
		Integer start = param.getStart();
		Integer length = param.getLength();
		Long toUserId = param.getConcurrentUserId();
		PageVo<DetailInvitTopicVo> result = new PageVo<DetailInvitTopicVo>();
		List<DetailInvitTopicVo> list = invitTopicDao.listAppend(toUserId, null, start, length);
		Integer total = invitTopicDao.countListAppend(toUserId, null);
		result.setItems(list);
		result.setPageNum(param.getPageNum());
		result.setPageSize(param.getPageSize());
		result.setTotal(total);
		return result;
	}
	
	
	public List<DetailInvitTopicVo> listTopic(Long invitId, Long parent) {
		List<DetailInvitTopicVo> list = invitTopicDao.listTopic(invitId, parent);
		return list;
	}
	
	
	/**
	 * 帖子Topic子集列表
	 * @param invitTopicId
	 * @return
	 * @throws WpsException
	 */
	public List<Long> listSon(List<Long> invitTopicIdList) throws WpsException {
		List<Long> result = invitTopicDao.listSon(invitTopicIdList);
		return result;
	}
	
	/**
	 * 帖子Topic详情
	 * @param invitTopicId
	 * @return
	 * @throws WpsException
	 */
	public Object detail(Long invitTopicId) throws WpsException {
		Object result = invitTopicDao.detail(invitTopicId);
		if (result == null) {
			throw new WpsException(ExceptionCode.数据不存在, "invitTopicId: " + String.valueOf(invitTopicId));
		}
		return result;
	}
	
	/**
	 * 评论树
	 * @param invitId
	 * @return
	 */
	public Object listTopicTree(Long invitId) {
		List<com.zhuss.opus.db.entity.InvitTopic> topicList = invitTopicDao.listAll(Area.东区.key(), invitId);
		List<ListInvitTopicTreeVo> result = new ArrayList<>();
		transferTree(topicList, result);
		return result;
	}
	
	/**
	 * 转换树
	 * @param topicList
	 * @param result
	 */
	private void transferTree(List<com.zhuss.opus.db.entity.InvitTopic> topicList, List<ListInvitTopicTreeVo> result) {
		for (com.zhuss.opus.db.entity.InvitTopic topic : topicList) {
			if (topic.getParent() == null || topic.getParent().equals(0l)) {
				ListInvitTopicTreeVo vo = new ListInvitTopicTreeVo();
				vo.setInvitTopicId(topic.getInvitTopicId());
				vo.setTopicContent(topic.getTopicContent());
				vo.setTargetUserId(topic.getTargetUserId());
				vo.setParent(topic.getParent());
				vo.setCreateTime(topic.getCreateTime());
				vo.setCreator(topic.getCreator());
				vo.setCreatorId(topic.getCreatorId());
				vo.setCreatorUrl(topic.getCreatorUrl());
				result.add(vo);
			}
		}
		
		for (ListInvitTopicTreeVo vo : result) {
			vo.setChildren(init(topicList, vo));
		}
	}
	
	/**
	 * 递归初始化树
	 * @param topicList
	 * @param vo
	 * @return
	 */
	private List<ListInvitTopicTreeVo> init(List<com.zhuss.opus.db.entity.InvitTopic> topicList, ListInvitTopicTreeVo vo) {
		ListBean<ListInvitTopicTreeVo> listBean = new ListBean<>();
		for (com.zhuss.opus.db.entity.InvitTopic topic : topicList) {
			if (vo.getInvitTopicId().equals(topic.getParent())) {
				ListInvitTopicTreeVo item = new ListInvitTopicTreeVo();
				item.setInvitTopicId(topic.getInvitTopicId());
				item.setTopicContent(topic.getTopicContent());
				item.setTargetUserId(topic.getTargetUserId());
				item.setParent(topic.getParent());
				item.setCreateTime(topic.getCreateTime());
				item.setCreator(topic.getCreator());
				item.setCreatorId(topic.getCreatorId());
				item.setCreatorUrl(topic.getCreatorUrl());
				item.setChildren(init(topicList, item));
				listBean.build(item);
			}
		}
		return listBean.getList();
	}
	
	
	

}
